package com.topmap.service.impl;

import com.topmap.dao.ArticleDOMapper;
import com.topmap.dataobject.ArticleDO;
import com.topmap.error.BusinessException;
import com.topmap.error.EmBusinessError;
import com.topmap.service.ArticleService;
import com.topmap.service.model.ArticleModel;
import com.topmap.validator.ValidationResult;
import com.topmap.validator.ValidatorImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: MaHuadong
 * @Date: 2019/7/18 13:15
 * @Version 1.0
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDOMapper articleDOMapper;


    //校验
    @Autowired
    private ValidatorImpl validator;

    @Override
    public void postArticle(ArticleModel articleModel) throws BusinessException {
        if (articleModel==null){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }

        //注解版校验方法
        ValidationResult result= validator.validator(articleModel);
        if (result.isHasError()){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,result.getErrorMsg());
        }


        //在这里我们要实现model转换成do的方法,因为我们dao层的mapper识别的是dataobject（do）文件
        ArticleDO articleDO=convertFromUserModel(articleModel);
        //把信息插入数据库

        try {
            articleDOMapper.insertSelective(articleDO);   //这个就没有问题
        }catch (DuplicateKeyException ex){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }
    }


    //获取所有的发帖文章
    @Override
    public List<ArticleModel> listArticle() {
        //使用mapper的listArticle方法查询所有的文章dataobject
        List<ArticleDO> articleDOList=articleDOMapper.listArticle();

        //使用java8的stream api把 feiyiDOList中的每一个都转化出来
        List<ArticleModel> articleModelList=articleDOList.stream().map(articleDO -> {
            //map成articleModel
            ArticleModel articleModel=this.convertModelFromDataObject(articleDO);
            return articleModel;
        }).collect(Collectors.toList());
        //返回出去articleModelList
        return articleModelList;
    }

    //把articleDO转换为articleModel的方法
    private ArticleModel convertModelFromDataObject(ArticleDO articleDO) {
        ArticleModel articleModel=new ArticleModel();

        //copy articleDO
        BeanUtils.copyProperties(articleDO,articleModel);

        return articleModel;


    }


    //在这里我们要实现model转换成do的方法,因为我们dao层的mapper识别的是dataobject（do）文件
    private ArticleDO convertFromUserModel(ArticleModel articleModel) {
        if (articleModel==null){
            return null;
        }

        ArticleDO articleDO=new ArticleDO();
        //  简单的使用copyProperties把对应usermodel的属性copy到userDO类中
        BeanUtils.copyProperties(articleModel,articleDO);

        return articleDO;
    }
}
